package com.hemerasolutions.v2trainer.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.hemerasolutions.v2trainer.component.interfaces.list.PhotosListCompInterface;
import com.hemerasolutions.v2trainer.component.interfaces.list.VideosListCompInterface;
import com.hemerasolutions.v2trainer.controller.interfaces.EquipmentBeanInterface;
import com.hemerasolutions.v2trainer.model.exercise.Equipment;
import com.hemerasolutions.v2trainer.service.ExceptionService;
import com.hemerasolutions.v2trainer.service.interfaces.EquipmentServiceInterface;



@Controller
@ManagedBean(name="equipmentBean")
@Scope("view")
public class EquipmentBean extends BaseBean implements EquipmentBeanInterface {


	private static final long serialVersionUID = 1L;

	private List<Equipment> equipments;
	
	private Equipment selEquipment;
	
	private Equipment equipment;
	
	@Autowired
	private PhotosListCompInterface photosListHandler;

	@Autowired
	private VideosListCompInterface videosListHandler;

	private Date 		begCreatedAt;
    
	private Date 		endCreatedAt;
    
	private Date 		begChangedAt;
    
	private Date 		endChangedAt;

	private String 		findByName;
    
	private String 		findByDescription;
    
	private String 		findByObs;
    
	private boolean 	findByStatus = true;	
	
	
	
	@Autowired
	private EquipmentServiceInterface equipmentService;
	

	
	public EquipmentBean() {
		super();
	}

	@PostConstruct
	public void init(){
		super.init();
		this.listAll();
	}

	private void listAll() {
    	Map<String,Object> fields = new HashMap<String, Object>();
    	
        fields.put("librarys", this.getActiveUserAllowedLibrarys());
		
	    this.equipments = this.equipmentService.searchEquipments(fields);		
		
	}

	public void toAddEquipment(){
		this.equipment = new Equipment();
		this.videosListHandler.setVideos(this.equipment.getVideos());
		this.videosListHandler.setVideoType(VideosListCompInterface.EQUIPMENT_VIDEO);
		this.videosListHandler.refresh();
		this.photosListHandler.setPhotoType(PhotosListCompInterface.EQUIPMENT_PHOTO);
		this.photosListHandler.setPhotos(this.equipment.getPhotos());
		this.photosListHandler.refresh();

		
	}

	public void toViewEquipment(){
		this.videosListHandler.setVideos(this.equipment.getVideos());
		this.videosListHandler.setVideoType(VideosListCompInterface.EQUIPMENT_VIDEO);
		this.videosListHandler.refresh();
		this.photosListHandler.setPhotoType(PhotosListCompInterface.EQUIPMENT_PHOTO);
		this.photosListHandler.setPhotos(this.equipment.getPhotos());
		this.photosListHandler.refresh();
	}
	
	public void createEquipment() throws ExceptionService{
		try {
			this.equipment.setChangedBy(this.getActiveUser());
			equipmentService.createEquipment(equipment);
			this.showInfoMessage(	this.localizedText("CreatedWithSuccess"),
					this.localizedText("TheEquipment") + " " +
					this.equipment.getName() + " " + 
					this.localizedText("wasCreatedWithSuccess"));			
			this.equipment = null;
			this.listAll();
		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("CreateError"), e.getLocalizedMessage());
		}
		
	}

	public void saveEquipment() throws ExceptionService{
		try {
			this.equipment.setChangedBy(this.getActiveUser());
			this.equipment.setCreatedAt(new Date());
			equipmentService.saveEquipment(equipment);
			this.showInfoMessage(	this.localizedText("SavedWithSuccess"),
					this.localizedText("TheEquipment") + " " +
					this.equipment.getName() + " " + 
					this.localizedText("wasSavedWithSuccess"));			
			this.listAll();
		} catch (ExceptionService e) {
			this.showErrorMessage(this.localizedText("SaveError"), e.getLocalizedMessage());
		}

	}
	
	public void deleteEquipment() throws ExceptionService{
		try{
			equipmentService.deleteEquipment(this.equipment);
			this.showInfoMessage(	this.localizedText("Deleted"),
					this.localizedText("UserDeletedWithSuccess"));
			this.equipments.remove(this.equipment);
			this.equipment = null;
		}catch(ExceptionService e){
			this.showErrorMessage(this.localizedText("DeleteError"), e.getLocalizedMessage());
		}
	}
	
	
	public void search(ActionEvent event){
    	Map<String,Object> fields = new HashMap<String, Object>();
    	
		if( this.begCreatedAt != null && this.endCreatedAt != null && 
				this.begCreatedAt.compareTo(this.endCreatedAt) > 0){
				this.showErrorMessage(	this.localizedText("CreationDate"),
										this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
				return;
			}
			

		if( this.begChangedAt != null && this.endChangedAt != null && 
			this.begChangedAt.compareTo(this.endChangedAt) > 0){
			this.showErrorMessage(	this.localizedText("ChangeDate"),
									this.localizedText("TheInitialDateNeedToBeGreaterThanFinal"));
			return;			
		}
    	
    	

    	if (!this.findByName.equals("")) {
            fields.put("name", this.findByName);
        }

    	if (!this.findByDescription.equals("")) {
            fields.put("description", this.findByDescription);
        }
    	
    	if (!this.findByObs.equals("")){
    		fields.put("obs",this.findByObs);
    	}
    	
    	if (this.begCreatedAt != null){
    		fields.put("begCreatedAt", this.begCreatedAt);
    	}
    	if (this.endCreatedAt != null){
            fields.put("endCreatedAt",this.endCreatedAt);
        }
        if (this.begChangedAt != null){
        	fields.put("begChangedAt",this.begChangedAt);
        }
        if (this.endChangedAt != null){
        	fields.put("endChangedAt", this.endChangedAt);
        }

        
        fields.put("librarys", this.getActiveUserAllowedLibrarys());
        
	    fields.put("active", this.findByStatus);
		
	    this.equipments = this.equipmentService.searchEquipments(fields);

		if((this.equipments).isEmpty()){
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.localizedText("NoEntrysFounded"));
		}else{
			this.showInfoMessage(	this.localizedText("SearchCompleted"),
									this.equipments.size() + " " + 
									this.localizedText("entrysFounded"));
		}
		
	}
	
	public void cancelChange(){
		if(this.equipment.getId() != 0){
			this.equipment = equipmentService.readEquipment(equipment);
		}else{
			this.equipment = new Equipment();
		}
	}
	
	
	
	
	public List<Equipment> getEquipments() {
		return equipments;
	}

	public Equipment getSelEquipment() {
		return selEquipment;
	}

	public Equipment getEquipment() {
		return equipment;
	}

	public void setEquipments(List<Equipment> equipments) {
		this.equipments = equipments;
	}

	public void setSelEquipment(Equipment selEquipment) {
		this.selEquipment = selEquipment;
	}

	public void setEquipment(Equipment equipment) {
		this.equipment = equipment;
	}

	public Date getBegCreatedAt() {
		return begCreatedAt;
	}

	public Date getEndCreatedAt() {
		return endCreatedAt;
	}

	public Date getBegChangedAt() {
		return begChangedAt;
	}

	public Date getEndChangedAt() {
		return endChangedAt;
	}

	public String getFindByName() {
		return findByName;
	}

	public String getFindByDescription() {
		return findByDescription;
	}

	public String getFindByObs() {
		return findByObs;
	}

	public boolean isFindByStatus() {
		return findByStatus;
	}

	public EquipmentServiceInterface getEquipmentService() {
		return equipmentService;
	}

	public void setBegCreatedAt(Date begCreatedAt) {
		this.begCreatedAt = begCreatedAt;
	}

	public void setEndCreatedAt(Date endCreatedAt) {
		this.endCreatedAt = endCreatedAt;
	}

	public void setBegChangedAt(Date begChangedAt) {
		this.begChangedAt = begChangedAt;
	}

	public void setEndChangedAt(Date endChangedAt) {
		this.endChangedAt = endChangedAt;
	}

	public void setFindByName(String findByName) {
		this.findByName = findByName;
	}

	public void setFindByDescription(String findByDescription) {
		this.findByDescription = findByDescription;
	}

	public void setFindByObs(String findByObs) {
		this.findByObs = findByObs;
	}

	public void setFindByStatus(boolean findByStatus) {
		this.findByStatus = findByStatus;
	}

	public void setEquipmentService(EquipmentServiceInterface equipmentService) {
		this.equipmentService = equipmentService;
	}

	public PhotosListCompInterface getPhotosListHandler() {
		return photosListHandler;
	}

	public void setPhotosListHandler(PhotosListCompInterface photosListHandler) {
		this.photosListHandler = photosListHandler;
	}

	public VideosListCompInterface getVideosListHandler() {
		return videosListHandler;
	}

	public void setVideosListHandler(VideosListCompInterface videosListHandler) {
		this.videosListHandler = videosListHandler;
	}

	
	
}
